package com.iyoudoock.citymine.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.adapter.BlacklistAdapter;
import com.iyoudoock.citymine.bean.BlacklistBean;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.adapter.OnChildClickListener;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BlackListActivity extends BaseActivity {
    @BindView(R.id.blacklist_lv)
    PullToRefreshListView blacklistLv;

    private BlacklistAdapter adapter;
    private ArrayList<BlacklistBean> items;
    private int currentPage = 1;
    private int currentItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_list);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        setTitle("黑名单");
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();
        items = new ArrayList<>();
        adapter = new BlacklistAdapter(items, this);
        this.blacklistLv.setAdapter(adapter);
        adapter.setOnChildClickListener(mOnChildClickListener);
        loadingData();
    }


    @Override
    public void loadingData() {
        params = HttpUtils.selectMyBlackOrCollect(mUid, Value.Type.BALCKLIST, currentPage, HttpUtils.PAGE_SIZE);
        mJsonUtils.post(HttpUtils.BALCKLIST_INFO_URL, params, this, Value.Flag.BLACKLIST_INFO);
    }


    /**
     * on child click
     */
    private OnChildClickListener mOnChildClickListener = new OnChildClickListener() {
        @Override
        public void onClick(View view, int i) {
            //handle remove issue
            BlacklistBean bean = items.get(i);
            removeFromBlacklist(bean.getUid());

            currentItem = i;
        }
    };


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            if (flag.equals(Value.Flag.BLACKLIST_INFO)) {
                String s = response.getString(Key.Tag.DATA);
                if (TextUtils.isEmpty(s)) return;
                List<BlacklistBean> result = JSON.parseArray(s, BlacklistBean.class);
                if (result == null) return;
                items.addAll(result);
                adapter.notifyDataSetChanged();
            } else if (flag.equals(Value.Flag.REMOVE_FROM_BLACKLIST)) {
                int state = response.getInt(Key.Tag.STATE);
                if (state == Value.Response.SUCCESS) {
                    items.remove(currentItem);
                    adapter.notifyDataSetChanged();
                    String msg = response.getString(Key.Tag.MSG);
                    ToastUtils.simpleToast(getActivity(), msg);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }
}
